<!-- Sharing Permissions -->

<%# initialization: required to keep javascript independent of the access type codes, to allow the use the constants instead -%>
<input type="hidden" id="const_private" value="<%= Policy::PRIVATE -%>"/>
<input type="hidden" id="const_everyone" value="<%= Policy::EVERYONE -%>"/>
<input type="hidden" id="const_all_registered_users" value="<%= Policy::ALL_REGISTERED_USERS -%>"/>
<input type="hidden" id="const_all_elab_users" value="<%= Policy::ALL_ELAB_USERS -%>"/>
<input type="hidden" id="const_custom_permissions_only" value="<%= Policy::CUSTOM_PERMISSIONS_ONLY -%>"/>

<input type="hidden" id="const_determined_by_group" value="<%= Policy::DETERMINED_BY_GROUP -%>"/>
<input type="hidden" id="const_no_access" value="<%= Policy::NO_ACCESS -%>"/>
<input type="hidden" id="const_viewing" value="<%= Policy::VIEWING -%>"/>
<input type="hidden" id="const_downloading" value="<%= Policy::DOWNLOADING -%>"/>
<input type="hidden" id="const_editing" value="<%= Policy::EDITING -%>"/>


<%# this hidden input will submit all permission data with the form -%>
<%= hidden_field_tag "sharing[permissions][contributor_types]", "" -%>
<%= hidden_field_tag "sharing[permissions][values]", "" -%>

<div class="fold">

  <div class="foldTitle">
    Sharing
  </div>

  <div class="foldContent" style="display: block;">

    <div class="box_infotext">
    	<p>
    		Here you can specify who can <b>view</b> and <b>download</b> this <% if @resource_type=="Script" -%>
                Script/Method
                <% elsif @resource_type=="Csvarchive" -%>
                Data Extract
                <% else -%>
                <%= @resource_type -%>
                <% end -%>

			</p>
			
		</div>

		<br/>
		
		<!-- private -->
	  <p><label for="sharing_scope_<%= Policy::PRIVATE -%>">
    	<input type="hidden" name="sharing[access_type_<%= Policy::PRIVATE -%>]" value="<%= Policy::NO_ACCESS -%>"/>
			<input type="hidden" name="sharing[include_custom_sharing_<%= Policy::PRIVATE -%>]" value="0"/>
			
			<input <%= 'checked="checked"' if @sharing_mode == Policy::PRIVATE %> id="sharing_scope_<%= Policy::PRIVATE -%>" name="sharing[sharing_scope]"
            value="<%= Policy::PRIVATE -%>" type="radio" onclick="javascript:setSharingElementVisibility(<%= Policy::PRIVATE -%>);" />
        - Keep this <% if @resource_type=="Script" -%>
                Script/Method
                <% else -%>
                <%= @resource_type -%>
                <% end -%> private.
		</label></p>
		
		
		<p style="font-weight: bold; margin-top: 1em;">Or share it with..</p>


		<% if @ukda_only!=nil && @ukda_only == true %>
		<p>This data extract contains variables from UKDA surveys and can only be shared with other MethodBox users</p>
		<% end %>
    <!-- all registered users -->
    <p>
    	<label for="sharing_scope_<%= Policy::ALL_REGISTERED_USERS -%>">
      	<input type="radio" <%= 'checked="checked"' if @sharing_mode == Policy::ALL_REGISTERED_USERS -%> id="sharing_scope_<%= Policy::ALL_REGISTERED_USERS -%>" name="sharing[sharing_scope]"
              value="<%= Policy::ALL_REGISTERED_USERS -%>" onclick="javascript:setSharingElementVisibility(<%= Policy::ALL_REGISTERED_USERS -%>);" />
          - All registered users for
		  </label>
		  <%= render :partial => "assets/sharing_form_access_type_dropdown", :locals => { :editing => @resource_type == "Survey" ? true : false, :access_type_selector_for_scope => Policy::ALL_REGISTERED_USERS,
			                                                                                :sharing_scope => @sharing_mode, :access_type => @access_mode } -%>
						
			<br/>
		</p>
		<!-- the world -->
		<div id="world_sharing_div">
		<% if @ukda_only!=nil && @ukda_only == false ||  @resource_type!="Survey"%>
	    <p>
	    	<label for="sharing_scope_<%= Policy::EVERYONE -%>">
	      	<input type="radio" <%= 'checked="checked"' if @sharing_mode == Policy::EVERYONE -%> id="sharing_scope_<%= Policy::EVERYONE -%>" name="sharing[sharing_scope]"
	              value="<%= Policy::EVERYONE -%>" onclick="javascript:setSharingElementVisibility(<%= Policy::EVERYONE -%>);" />
	          - Everyone ie. the world
			  </label>
			  <%= render :partial => "assets/sharing_form_access_type_dropdown", :locals => { :editing => false, :access_type_selector_for_scope => Policy::EVERYONE,
				                                                                                :sharing_scope => @sharing_mode, :access_type => @access_mode } -%>

				<br/>
			</p>
		<% end %>
		</div>
		<p>
	    	<label for="sharing_scope_<%= Policy::CUSTOM_PERMISSIONS_ONLY -%>">
	      	<input type="radio" <%= 'checked="checked"' if @sharing_mode == Policy::CUSTOM_PERMISSIONS_ONLY -%> id="include_custom_sharing_<%= Policy::CUSTOM_PERMISSIONS_ONLY -%>" name="sharing[sharing_scope]"
	              value="<%= Policy::CUSTOM_PERMISSIONS_ONLY-%>" onclick="javascript:setSharingElementVisibility(<%= Policy::CUSTOM_PERMISSIONS_ONLY-%>);" />
	          - Specific Groups
			  </label>

				<br/>

<!-- this next line would use a helper action to show a redbox popup for adding a new group.  The problem is in the
request sent.  Serialising params for a link_to_remote isn't obvious or easy for array style hidden inputs.  Maybe can add this back in some time in the future.  Just add groups through standard views at the moment -->

				<%#= work_group_popup_link_action_new() -%>
				<div id="specific_sharing" <%= @sharing_mode == Policy::CUSTOM_PERMISSIONS_ONLY ? hide_style(false) : hide_style(true) -%>>
					<p>          
						<b>Share with group(s):</b>
						<div style="clear:both;"></div>
					<%= render :partial => "assets/workgroup_selector", :locals => { :resource_id => "groups", :resource_type => "Groups",   # this is only for presentation, not for any processing
				      :collection=>groups,:selected=>selected_groups,:authorization_for_showing_already_done => false } -%>
				</p>
				</div>
				<br/>
				<div style="clear:both;">
				</div>

			</p>
		
  </div>
</div>

<script type="text/javascript">
	init_sharing();
	
	GET_POLICY_DEFAULTS_LINK = '<%= request_policy_settings_path -%>';
  GET_INSTITUTIONS_LINK = '<%= request_institutions_projects_path -%>';

	
	CREATE_FAVOURITE_GROUP_LINK = '<%= create_favourite_group_path -%>';
	UPDATE_FAVOURITE_GROUP_LINK = '<%= update_favourite_group_path -%>';
	
	
	
	<% @policy.permissions.each do |p| -%>
	  <% case p.contributor_type; when "FavouriteGroup", "Person", "Project", "Institution", "WorkGroup" -%>
		  <% unless p.contributor_type == "FavouriteGroup" && [FavouriteGroup::WHITELIST_NAME, FavouriteGroup::BLACKLIST_NAME].include?(p.contributor.name) -%>
			  if('<%= p.contributor_type -%>' in permissions_for_set)
				  permissions_for_set['<%= p.contributor_type -%>']++;
			  else {
					permissions_for_set['<%= p.contributor_type -%>'] = 0;
					permission_settings['<%= p.contributor_type -%>'] = new Array();
				}
				
				<% if p.contributor_type == "Person" -%>
				  <% contributor_name = (p.contributor.first_name + " " + p.contributor.last_name) -%>
				<% elsif p.contributor_type == "WorkGroup" -%>
				  <% contributor_name = p.contributor.project.name + " @ " + p.contributor.institution.name -%>
				<% else -%>
				  <% contributor_name = p.contributor.name -%>
				<% end -%>
				
				// add current values into the associative array of permissions:
        // first index is the category of contributor type of the permission, the second - consecutive
        // number of occurrences of permissions for such type of contributor
				permission_settings['<%= p.contributor_type -%>'][permissions_for_set['<%= p.contributor_type -%>']] =
				  ['<%= contributor_name -%>', <%= p.contributor_id -%>, <%= p.access_type -%>];
			<% end -%>
		<% end -%>
	<% end -%>
	
	// build custom permissions list and set relevant other options
	//this is not used anymore in methodbox
	// updateCustomSharingSettings();
</script>